<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        var websocket;
        if (window.WebSocket) {

            websocket = new WebSocket("ws://localhost:8999/ws");
            websocket.onmessage = function (event) {
                var ta = document.getElementById("responseText");
                ta.value = ta.value + "\n" + event.data;
            }
            websocket.onopen = function(event){
                var ta = document.getElementById("responseText");
                ta.value = "连接开启";
            }
            websocket.onclose = function(event){
                var ta = document.getElementById("responseText");
                ta.value = ta.value + "\n" + "连接关闭";
            }
        }else{
            alert('浏览器不支持websocket')
        }
        function send(message){
            if(!window.WebSocket){
                return;
            }
            if( websocket.readyState == WebSocket.OPEN ){
                websocket.send(message);
            }else{
                alert('连接尚未开启');
            }
        }

    </script>
</head>

<body>
<form onsubmit="return false">
    <textarea name="message" style="width:400px;height:200px"></textarea>
    <input type="button" value="发送数据" onclick="send(this.form.message.value)"/>
    <h3>服务器输出</h3>
    <textarea id="responseText" style="width:400px;height:200px"></textarea>
    <input type="button" value="清空内容" onclick="javascript:document.getElementById('responseText').value=''"/>
</form>

</body>
</html>